package com.itheima.work;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

import static java.lang.System.*;

/**
 * @auther Macrydy
 * @date 2020/7/7 19:21
 */
public class Test4 {

    public static void main(String[] args) {
        ThreadDemo4 td = new ThreadDemo4();

        Thread t1 = new Thread(td, "张三");
        Thread t2 = new Thread(td, "李四");
        Thread t3 = new Thread(td, "王五");
        t1.start();
        t2.start();
        t3.start();

        out.println();
    }

}

class ThreadDemo4 extends Thread{
    private static String[] box = {"电视机","电冰箱","电脑","游戏机","洗衣机","空调","手机","平板电脑","电动车","电饭煲"};
    private static List<String> list = new ArrayList<>();
    private ReentrantLock lock = new ReentrantLock();
    private  List<String> list1 = new ArrayList<>();
    private  List<String> list2 = new ArrayList<>();
    private  List<String> list3 = new ArrayList<>();

    @Override
    public void run() {

        while (true){
            lock.lock();
            int i = new Random().nextInt(box.length);
            if (list.size()==box.length){
                return;
            }
            if (!list.contains(box[i])){
                list.add(box[i]);

                String name = Thread.currentThread().getName();
                if ("张三".equals(name)){
                    list1.add(box[i]);
                }
                if ("李四".equals(name)){
                    list2.add(box[i]);
                }
                if ("王五".equals(name)){
                    list3.add(box[i]);
                }
                out.println(Thread.currentThread().getName()+"抽到了"+box[i]);
            }
            lock.unlock();
        }
//        System.out.println(list1);

    }
}